﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Police_TransferSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    private void init()
    {

        BindTransferType(cblDR, "SBDD10");
        BindTransferType(cblDC, "SBDD20");
        StartTime.Text = DateTime.Today.ToString("yyyy-01-01");
        EndTime.Text = DateTime.Today.ToString("yyyy-MM-dd");
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!isOfferSet())
        {
            gvTransfer.Columns[5].Visible = false;
            gvTransfer.Columns[8].Visible = false;
        }
    }



    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindTransfer(PageIndex);
    }
    private void BindTransferType(CheckBoxList cbl, string parentID)
    {
        List<CodeItemInfo> codeItems = CodeItem.GetList("parentID='" + parentID + "' order by seqID");
        cbl.DataTextField = "ItemName";
        cbl.DataValueField = "ItemID";
        cbl.DataSource = codeItems;
        cbl.DataBind();

    }

    private string GetDepartmentID()
    {
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
            departmentID = departmentID.Substring(0, 2) + "00000";
        else
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400102"))
                departmentID = departmentID.Substring(0, 4) + "000";
        return departmentID;
    }

    protected void lkbQurey_Click(object sender, EventArgs e)
    {
        lblWhere.Text = "";
        string whereDR = "";
        foreach (ListItem item in cblDR.Items)
        {
            if (item.Selected)
            {
                whereDR += "'" + item.Value + "',";
            }
        }
        string whereDC = "";
        foreach (ListItem item in cblDC.Items)
        {
            if (item.Selected)
            {
                whereDC += "'" + item.Value + "',";
            }
        }
        string departmentID = GetDepartmentID();
        if (whereDR != "")
        {
            if (isAdmin())
            {
                lblWhere.Text += " FromJingZhong in (" + whereDR.Substring(0, whereDR.Length - 1) + ")";
            }
            else if (isOfferSet())
            {
                lblWhere.Text += "(FromJingZhong in (" + whereDR.Substring(0, whereDR.Length - 1) + ") and ToUnitID like '" + departmentID.Substring(0, 4) + "%')";
            }
            else
            {
                lblWhere.Text += "(FromJingZhong in (" + whereDR.Substring(0, whereDR.Length - 1) + ") and ToUnitID = '" + departmentID + "')";
            }
        }
        if (whereDC != "")
        {
            lblWhere.Text += lblWhere.Text == "" ? "" : " or ";
            if (isAdmin())
            {
                lblWhere.Text += " ToJingZhong in (" + whereDC.Substring(0, whereDC.Length - 1) + ")";
            }
            else if (isOfferSet())
            {
                lblWhere.Text += "(ToJingZhong in (" + whereDC.Substring(0, whereDC.Length - 1) + ") and FromUnitID like '" + departmentID.Substring(0, 4) + "%')";
            }
            else
            {
                lblWhere.Text += "(ToJingZhong in (" + whereDC.Substring(0, whereDC.Length - 1) + ") and FromUnitID='" + departmentID + "')";
            }
        }
        if (cbxNB.Checked)
        {
            if (isAdmin())
            {
                lblWhere.Text += lblWhere.Text == "" ? "" : " or ";
                lblWhere.Text += " (LEFT(ToUnitID, 4) <> LEFT(FromUnitID, 4)) AND (FromJingZhong = 'SBDD30') AND (ToJingZhong = 'SBDD30') ";
            }
            else if (isOfferSet())
            {
                lblWhere.Text += lblWhere.Text == "" ? "" : " or ";
                lblWhere.Text += "( ToJingZhong='SBDD30' and FromJingZhong='SBDD30' and  (ToUnitID like '" + departmentID.Substring(0, 4) + "%' or FromUnitID like '" + departmentID.Substring(0, 4) + "%' ))";
            }
            else
            {
                lblWhere.Text += lblWhere.Text == "" ? "" : " or ";
                lblWhere.Text += "( ToJingZhong='SBDD30' and FromJingZhong='SBDD30' and  ((ToUnitID='" + departmentID + "' and  FromUnitID like '" + departmentID.Substring(0, 4) + "%' ) or (FromUnitID='" + departmentID + "' and ToUnitID like '" + departmentID.Substring(0, 4) + "%' )))";
            }
        }

        string sjwhere = "";

        if (StartTime.Text != "")
        {
            sjwhere += sjwhere == "" ? "" : " and ";
            sjwhere += "TransferDate>=  '" + StartTime.Text + "'";
        }
        if (EndTime.Text != "")
        {
            sjwhere += sjwhere == "" ? "" : " and ";
            sjwhere += "TransferDate<  '" + Convert.ToDateTime(EndTime.Text).AddDays(1).ToString() + "'";
        }
        if (lblWhere.Text == "")
        {
            if (isAdmin())
            {
            }
            else
                if (isOfferSet())
                {
                    lblWhere.Text += "(ToUnitID like '" + departmentID.Substring(0, 4) + "%' or FromUnitID like '" + departmentID.Substring(0, 4) + "%' )";
                }
                else
                {
                    lblWhere.Text += "(ToUnitID = '" + departmentID + "%' or FromUnitID = '" + departmentID + "%')";
                }
        }
        lblWhere.Text = lblWhere.Text == "" ? "" : "(" + lblWhere.Text + ") and ";
        lblWhere.Text += sjwhere;

        ListPageContral1.SetFirstPage();
        BindTransfer(0);


    }

    bool isAdmin()
    {
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool isOfferSet()
    {
        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400101") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400301") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400302"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void gvTransfer_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string TID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["TransferID"].ToString();
        string ID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["SoldierID"].ToString();
        Response.Redirect("TransfeSoldier.aspx?ID=" + ID + "&TID=" + TID);
    }
    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Soldier soldier = new Soldier();


            string ppid = gvTransfer.DataKeys[e.Row.RowIndex].Values["SoldierID"].ToString();
            Yipusoft.SoldierArchives.Model.SoldierArchiveInfo info = soldier.GetSoldierInfoByID(ppid);
            if (info != null)
            {
                e.Row.Cells[1].Text = soldier.GetSoldierInfoByID(ppid).SoldierName;
            }
            Label ID1 = (Label)e.Row.FindControl("ID");

            ID1.Text = ((int)ListPageContral1.CurrentPageIndex * 20 + e.Row.RowIndex + 1).ToString();


            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            ImageButton btnview = e.Row.FindControl("btnview") as ImageButton;
            string trid = gvTransfer.DataKeys[e.Row.RowIndex].Values["TransferID"].ToString();
            TranslateLogInfo loginfo = TranslateLogs.GetDetailByDID(new Guid(trid));
            if (loginfo != null)
            {

                //查看

                btnview.PostBackUrl = "TranslateProcess/TanslateDetail.aspx?Fid=" + loginfo.FID;
            }
            else
            {
                btnview.Visible = false;
            }
        }

    }

    private void BindTransfer(int pageIndex)
    {
        int totalCount = TransferSoldier.GetTransfersCountByWhere(lblWhere.Text);
        ListPageContral1.ItemCount = totalCount;
        gvTransfer.DataSource = TransferSoldier.GetTransfersByWhere(lblWhere.Text, 20, pageIndex);
        gvTransfer.DataBind();
    }
}
